import {
	setToken,
	getToken
} from '@/utils/token.js'
import {
	searchOneNew,
	queryCraftsmanById,
} from "@/common/api/auth.js"
// #ifdef H5
import {
	onShareData
} from '@/utils/share.js'
// #endif
// #ifdef MP-WEIXIN
import {
	shareMixins
} from '@/mixins/share'
import store from '..'
// #endif
const state = {
	// #ifdef MP-WEIXIN
	mixins: [shareMixins],
	// #endif
	shareData: {
		title: '名片王'
	},
	token: uni.getStorageSync("token") || "",
	isLogin: uni.getStorageSync("isLogin") || false, // 是否登陆
	userInfo: uni.getStorageSync("userInfo") || {}, // 用户信息
	userData: {}, //用户相关信息
	setType: 1,
	authType: '', // smsLogin:手机号登录注册, accountLogin:密码登录, forgotPwd:忘记密码, changePwd:修改密码, bindMobile:绑定手机号
}

const getters = {
	token: state => state.token,
	isLogin: state => state.isLogin,
	userInfo: state => state.userInfo,
	userOtherData: state => state.userData,
	authType: state => state.authType,
	shareData: state => state.shareData
}

const actions = {
	// 获取用户信息
	queryCraftsmanById({
		commit,
		state
	}, sendData) {
		if (sendData.userId) {
			let params = {
				craftsmanId: sendData.userId
			}
			params['isShare'] = sendData.isShare ? 1 : ''
			return queryCraftsmanById(params).then(res => {
				if (res.data && res.data.craftsmanId) {
					if (sendData.setShare == false) {
						return res.data
					} else {
						state.setType = 1
						commit('toSetShare', res.data)
						return res.data
					}
				} else {
					uni.showToast({
						title: '用户信息获取失败',
						icon: 'none'
					})
					return false
				}
			})
		}
	},
	// 获取企业
	searchOneNew({
		commit,
		state
	}, sendData) {
		// console.log("searchOneNew uni.getStorageSync('token')\n\n\n\n\n\n", uni.getStorageSync('token'));
		console.log("sendData", Number(sendData.userId));
		// console.log("substr(sendData.userId)", sendData.userId.substr(0,1));
		if (sendData.userId && Number(sendData.userId) >= 20000000) {
			let params = {
				companyId: sendData.userId
			}
			params['isShare'] = 1;
			return searchOneNew(params).then(res => {
				if (res.data && res.data.companyId) {
					if (sendData.setShare == false) {
						return res.data
					} else {
						state.setType = 2
						commit('toSetShare', res.data)
						return res.data;
					}
				} else {
					uni.showToast({
						title: '企业信息获取失败',
						icon: 'none'
					})
					return false
				}
			})
		}
	},
	// 获取用户信息
	getUserInfo({
		commit,
		dispatch,
		getters,
		state
	}, res) {
		let lastLoginStatus = getters.isLogin;
		res && commit('userInfo', res.data);
		commit('setIsLogin', true);
		dispatch('showAuthModal', '');
		if (!uni.getStorageSync("useNum") && res.data && res.data.craftsmanId || (res.data && res.data
				.craftsmanId != uni.getStorageSync("useNum"))) {
			uni.setStorageSync("useNum", res.data.craftsmanId || '')
			uni.setStorageSync("version", 1)
		}
		// #ifdef H5
		let loadUrl = window.location.href
		window.location.href = loadUrl
		// #endif
	},

	// 登录弹窗控制
	showAuthModal({
		commit,
		state
	}, type = 'accountLogin') {
		// console.log('111111111111111111111111111111111111111111', type)
		// #ifdef MP-WEIXIN
		let pages = getCurrentPages();
		let currentPage = pages[pages.length - 1];
		let currPathWx = currentPage.$page.fullPath;
		// console.log('currPathWx', currPathWx)
		setToken('jumpUrl', currPathWx)
		// #endif
		// #ifdef H5
		let currPathH5 = window.location.href
		// console.log('currPathH5', currPathH5)
		setToken('jumpUrl', currPathH5);
		// #endif
		setToken('AUTH_TYPE', type);
		commit('AUTH_TYPE', type);
	},

	// 退出登录
	unLogin({
		commit,
		dispatch
	}) {
		commit('setIsLogin', false);
		setToken('token', '');
		setToken('complogin', '');
		uni.showToast({
			title: '退出成功',
			icon: 'none'
		})
		// uni.showTabBar()
		// setToken('AUTH_TYPE', 'accountLogin');
		// commit('AUTH_TYPE', 'accountLogin');
		// let loadUrl =  window.location.href;
		// console.log('loadUrl', loadUrl);
		// setToken('jumpUrl', loadUrl);
	},
}

const mutations = {
	//设置分享参数
	setShareData(state, data) {
		state.shareData = data
	},
	// 初始化用户分享
	initShare(state) {
		//console.log('初始化分享')
		let acData = {}
		if (getToken('version') == 1) {
			acData = getToken('userInfo')
		} else {
			acData = getToken('complogin')
		}
		state.shareData = {
			title: `我是${getToken('version') == 1?acData.realName:acData.companyNameCn},邀请您与我交换名片 `, // 分享标题
			desc: getToken('version') == 1 ? (acData.industryTypeName || acData.remarks || '快来完善属于自己的名片吧！') : (
				acData.remarks || '快来完善属于自己的名片吧！'), // 分享描述
		}
		let shareUrl = ''
		// #ifdef H5
		shareUrl = `https://job.ydylmold.cn/mingpian/index.html#/pages/index/cardIndex/detailIndex?isshare=1`
		// #endif
		// #ifdef MP-WEIXIN
		shareUrl = `/pages/index/cardIndex/detailIndex?isshare=1`
		// #endif
		let sendObj = {
			isShare: 1,
			sharePhone: getToken('userInfo')['contactNumber'],
			shareEmail: getToken('userInfo')['email'],
			shareName: getToken('userInfo')['realName'],
			useNum: getToken('version') == 1 ? acData.craftsmanId : acData.companyId,
			moduleNo: acData.moduleNo,
			version: getToken('version')
		}
		shareUrl = shareUrl + `&sendObj=${encodeURIComponent(JSON.stringify(sendObj))}`
		state.shareData.path = shareUrl

		// #ifdef H5
		if (getToken('version') == 1) {
			state.shareData.imgUrl = acData.headImg ?
				'https://www.ydylmold.cn/images/ydylapp/usrCraftsman/image/' + acData.headImg :
				'https://job.ydylmold.cn/mingpian/index.html#/'
		} else {
			state.shareData.imgUrl = acData.companyLogo ?
				'https://www.ydylmold.cn/images/ydylapp/usrCompany/image/' + acData.companyLogo :
				'https://job.ydylmold.cn/mingpian/index.html#/'
		}
		onShareData(state.shareData)
		// #endif
	},
	// 设置用户分享
	toSetShare(state, data = {}) {
		let shareData = {}
		let shareUrl = ''
		// #ifdef H5
		shareUrl = shareData.path || window.location.href
		// #endif
		// #ifdef MP-WEIXIN
		let pages = getCurrentPages();
		let currentPage = pages[pages.length - 1];
		shareUrl = currentPage.$page.fullPath;
		// #endif
		let sendObj = {
			isShare: 1,
			sharePhone: getToken('userInfo')['contactNumber'],
			shareEmail: getToken('userInfo')['email'],
			shareName: getToken('userInfo')['realName'],
			useNum: state.setType == 1 ? data.craftsmanId : data.companyId,
			moduleNo: data.moduleNo,
			version: state.setType
		}
		//console.log(sendObj,'sendObj',data.companyId,state.setType)
		if (shareUrl.includes('/pages/index/index')) {
			// #ifdef H5
			shareUrl = `https://job.ydylmold.cn/mingpian/index.html#/pages/index/cardIndex/detailIndex?isshare=1`
			// #endif
			// #ifdef MP-WEIXIN
			shareUrl = `/pages/index/cardIndex/detailIndex?isshare=1`
			// #endif
			//console.log(6666)
		}
		if (shareUrl.includes('?')) {
			shareUrl = shareUrl + `&sendObj=${encodeURIComponent(JSON.stringify(sendObj))}`
		} else {
			shareUrl = shareUrl + `?sendObj=${encodeURIComponent(JSON.stringify(sendObj))}`
		}

		shareData.path = shareUrl
		if (getToken('version') == 1) {
			shareData.title = `我是${data.usrName},邀请您与我交换名片 `
			shareData.desc = data.remarks || '快来完善您的名片吧！'
			// #ifdef H5
			shareData.imgUrl = data.headImg ?
				'https://www.ydylmold.cn/images/ydylapp/usrCraftsman/image/' + data.headImg : ''
			onShareData(shareData)
			// #endif
		} else if (getToken('version') == 2) {
			shareData.title = `我是${data.companyNameCn},邀请您与我交换名片`
			shareData.desc = ((data && data.industryTypeName) || (data && data.remarks)) ||
				'快来完善属于自己的名片吧！'
			// #ifdef H5
			shareData.imgUrl = data.companyLogo ?
				'https://www.ydylmold.cn/images/ydylapp/usrCompany/image/' + data.companyLogo : ''
			onShareData(shareData)
			// #endif
		}

		state.shareData = shareData
		//console.log(shareData,88888)
		return shareData
	},
	token(state, payload) {
		state.token = payload;
		uni.setStorageSync("token", payload);
	},
	// 登录态
	setIsLogin(state, data) {
		state.isLogin = data
		setToken('isLogin', data)
	},
	// 用户信息
	userInfo(state, data) {
		state.userInfo = data;
		uni.setStorageSync("userInfo", data);
	},
	AUTH_TYPE(state, data) {
		data ? uni.hideTabBar() : uni.showTabBar();
		state.authType = data;
	},
	setShareWx(state, data) {
		state.shareData = data
	},

}

export default {
	state,
	mutations,
	actions,
	getters
}